package com.tang.common.exception;

import com.tang.common.exception.eumes.Error;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 * @Author tang jun liang
 * @Date 2023/10/18 10:45
 */
public final class BusinessAssert {
    private static final Map<String, Pattern> PATTERNS = new ConcurrentHashMap();

    public BusinessAssert() {
    }

    public static void isNull(Object value) {
        if (value != null) {
            throwIllegalArgumentException();
        }
    }

    public static void isNull(Object value, String message) {
        if (value != null) {
            throwIllegalArgumentException(message);
        }
    }

    public static void isNull(Object value, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void notNull(Object value, Error.ErrorCodeMessage errorCodeMessage) {
        if (value == null) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void notNull(Object value) {
        if (value == null) {
            throwIllegalArgumentException();
        }
    }

    public static void notNull(Object value, String message) {
        if (value == null) {
            throwIllegalArgumentException(message);
        }
    }

    public static void isEmpty(String value) {
        if (!StringUtils.isEmpty(value)) {
            throwIllegalArgumentException();
        }
    }

    public static void isEmpty(String value, String message) {
        if (!StringUtils.isEmpty(value)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void isEmpty(String value, Error.ErrorCodeMessage errorCodeMessage) {
        if (!StringUtils.isEmpty(value)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void notEmpty(String value) {
        if (StringUtils.isEmpty(value)) {
            throwIllegalArgumentException();
        }
    }

    public static void notEmpty(String value, String message) {
        if (StringUtils.isEmpty(value)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void notEmpty(String value, Error.ErrorCodeMessage errorCodeMessage) {
        if (StringUtils.isEmpty(value)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void isEmpty(Collection<?> value) {
        if (!CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException();
        }
    }

    public static void isEmpty(Collection<?> value, String message) {
        if (!CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void isEmpty(Collection<?> value, Error.ErrorCodeMessage errorCodeMessage) {
        if (!CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void notEmpty(Collection<?> value) {
        if (CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException();
        }
    }

    public static void notEmpty(Collection<?> value, String message) {
        if (CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void notEmpty(Collection<?> value, Error.ErrorCodeMessage errorCodeMessage) {
        if (CollectionUtils.isEmpty(value)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void notBlank(String value) {
        if (value == null || value.trim().isEmpty()) {
            throwIllegalArgumentException();
        }
    }

    public static void notBlank(String value, String message) {
        if (value == null || value.trim().isEmpty()) {
            throwIllegalArgumentException(message);
        }
    }

    public static void notBlank(String value, Error.ErrorCodeMessage errorCodeMessage) {
        if (value == null || value.trim().isEmpty()) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void eq(Object value, Object that) {
        if (value != null && !value.equals(that)) {
            throwIllegalArgumentException();
        }
    }

    public static void eq(Object value, Object that, String message) {
        if (value != null && !value.equals(that)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void eq(Object value, Object that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && !value.equals(that)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void ne(Object value, Object that) {
        if (value != null && value.equals(that)) {
            throwIllegalArgumentException();
        }
    }

    public static void ne(Object value, Object that, String message) {
        if (value != null && value.equals(that)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void ne(Object value, Object that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.equals(that)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static <T extends Comparable<T>> void lt(T value, T that) {
        if (value != null && value.compareTo(that) >= 0) {
            throwIllegalArgumentException();
        }
    }

    public static <T extends Comparable<T>> void lt(T value, T that, String message) {
        if (value != null && value.compareTo(that) >= 0) {
            throwIllegalArgumentException(message);
        }
    }

    public static <T extends Comparable<T>> void lt(T value, T that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.compareTo(that) >= 0) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static <T extends Comparable<T>> void lte(T value, T that) {
        if (value != null && value.compareTo(that) > 0) {
            throwIllegalArgumentException();
        }
    }

    public static <T extends Comparable<T>> void lte(T value, T that, String message) {
        if (value != null && value.compareTo(that) > 0) {
            throwIllegalArgumentException(message);
        }
    }

    public static <T extends Comparable<T>> void lte(T value, T that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.compareTo(that) > 0) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static <T extends Comparable<T>> void gt(T value, T that) {
        if (value != null && value.compareTo(that) <= 0) {
            throwIllegalArgumentException();
        }
    }

    public static <T extends Comparable<T>> void gt(T value, T that, String message) {
        if (value != null && value.compareTo(that) <= 0) {
            throwIllegalArgumentException(message);
        }
    }

    public static <T extends Comparable<T>> void gt(T value, T that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.compareTo(that) <= 0) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static <T extends Comparable<T>> void gte(T value, T that) {
        if (value != null && value.compareTo(that) < 0) {
            throwIllegalArgumentException();
        }
    }

    public static <T extends Comparable<T>> void gte(T value, T that, String message) {
        if (value != null && value.compareTo(that) < 0) {
            throwIllegalArgumentException(message);
        }
    }

    public static <T extends Comparable<T>> void gte(T value, T that, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.compareTo(that) < 0) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static <T extends Comparable<T>> void between(T value, T min, T max) {
        if (value != null && (value.compareTo(min) < 0 || value.compareTo(max) > 0)) {
            throwIllegalArgumentException();
        }
    }

    public static <T extends Comparable<T>> void between(T value, T min, T max, String message) {
        if (value != null && (value.compareTo(min) < 0 || value.compareTo(max) > 0)) {
            throwIllegalArgumentException(message);
        }
    }

    public static <T extends Comparable<T>> void between(T value, T min, T max, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && (value.compareTo(min) < 0 || value.compareTo(max) > 0)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void maxlength(String value, int max) {
        if (value != null && value.length() > max) {
            throwIllegalArgumentException();
        }
    }

    public static void maxlength(String value, int max, String message) {
        if (value != null && value.length() > max) {
            throwIllegalArgumentException(message);
        }
    }

    public static void maxlength(String value, int max, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.length() > max) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void minlength(String value, int min) {
        if (value != null && value.length() < min) {
            throwIllegalArgumentException();
        }
    }

    public static void minlength(String value, int min, String message) {
        if (value != null && value.length() < min) {
            throwIllegalArgumentException(message);
        }
    }

    public static void minlength(String value, int min, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && value.length() < min) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void length(String value, int min, int max) {
        if (value != null && (min > value.length() || value.length() > max)) {
            throwIllegalArgumentException();
        }
    }

    public static void length(String value, int min, int max, String message) {
        if (value != null && (min > value.length() || value.length() > max)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void length(String value, int min, int max, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && (min > value.length() || value.length() > max)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void in(Object value, Collection<?> collection) {
        if (value != null && !collection.contains(value)) {
            throwIllegalArgumentException();
        }
    }

    public static void in(Object value, Collection<?> collection, String message) {
        if (value != null && !collection.contains(value)) {
            throwIllegalArgumentException(message);
        }
    }

    public static void in(Object value, Collection<?> collection, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && !collection.contains(value)) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void pattern(String value, Pattern regex) {
        if (value != null && !regex.matcher(value).matches()) {
            throwIllegalArgumentException();
        }
    }

    public static void pattern(String value, Pattern regex, String message) {
        if (value != null && !regex.matcher(value).matches()) {
            throwIllegalArgumentException(message);
        }
    }

    public static void pattern(String value, String regex) {
        Pattern compiled = (Pattern)PATTERNS.get(regex);
        if (compiled == null) {
            PATTERNS.put(regex, compiled = Pattern.compile(regex));
        }

        pattern(value, compiled);
    }

    public static void pattern(String value, Pattern regex, Error.ErrorCodeMessage errorCodeMessage) {
        if (value != null && !regex.matcher(value).matches()) {
            throwIllegalArgumentException(errorCodeMessage);
        }
    }

    public static void pattern(String value, String regex, String message) {
        Pattern compiled = (Pattern)PATTERNS.get(regex);
        if (compiled == null) {
            PATTERNS.put(regex, compiled = Pattern.compile(regex));
        }

        pattern(value, compiled, message);
    }

    public static void pattern(String value, String regex, Error.ErrorCodeMessage errorCodeMessage) {
        Pattern compiled = (Pattern)PATTERNS.get(regex);
        if (compiled == null) {
            PATTERNS.put(regex, compiled = Pattern.compile(regex));
        }

        pattern(value, compiled, errorCodeMessage);
    }

    public static void isPrimaryKey(Number key) {
        if (key == null || key.longValue() <= 0L) {
            throwIllegalArgumentException();
        }
    }

    public static void isPrimaryKey(Number key, String message) {
        if (key == null || key.longValue() <= 0L) {
            throwIllegalArgumentException(message);
        }
    }

    public static void isPrimaryKey(Number key, Error.ErrorCodeMessage ErrorCodeMessage) {
        if (key == null || key.longValue() <= 0L) {
            throwIllegalArgumentException(ErrorCodeMessage);
        }
    }



    public static void throwIllegalArgumentException() {
        throw new BusinessException(Error.ARGUMENT_ERROR);
    }

    public static void throwIllegalArgumentException(String message) {
        if (StringUtils.isNotBlank(message)) {
            throw new BusinessException( message);
        } else {
            throwIllegalArgumentException();
        }
    }

    public static void throwIllegalArgumentException(Error.ErrorCodeMessage errorCodeMessage) {
        if (errorCodeMessage == null) {
            throwIllegalArgumentException();
        }

        throw new BusinessException(errorCodeMessage);
    }
}
